package com.fling.abcde.wxapi.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.Toast;

import com.fling.abcde.bean.WxOrderResBean;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WxApiUtils {

	public static IWXAPI sWxApi;
	public static final String WX_APP_ID = "wx5bbe398ab56040ae";
	public static final String WX_APP_SECRET = "ac06e77f1ec249e0159940bdf7969882";

	/**
	 * 微信登录
	 */
	public static void WxLogin(Context context) {

		if (judgeCanGo(context)) {
			return;
		}
		SendAuth.Req req = new SendAuth.Req();
		req.scope = "snsapi_userinfo";
		req.state = "zzc_login";
		sWxApi.sendReq(req);
	}

	public static void WxPay(WxOrderResBean bean, Context context) {

		if (judgeCanGo(context)) {
			return;
		}
		PayReq req = new PayReq();
		req.appId = bean.getAppid();
		req.partnerId = bean.getPartnerid();
		req.prepayId = bean.getPrepayid();
		req.nonceStr = bean.getNoncestr();
		req.timeStamp = bean.getTimestamp() + "";
		req.packageValue = bean.getPackage();
		req.sign = bean.getSign();
		sWxApi.sendReq(req);
	}

	/**
	 * 分享文本至朋友圈
	 *
	 * @param text
	 * 	文本内容
	 * @param judge
	 * 	类型选择 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
	 */
	public static void WxTextShare(Context context, String text, int judge) {

		if (judgeCanGo(context)) {
			return;
		}
		WXTextObject textObject = new WXTextObject();
		textObject.text = text;

		WXMediaMessage message = new WXMediaMessage();
		message.mediaObject = textObject;
		message.description = text;

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = message;
		req.scene = judge;

		sWxApi.sendReq(req);
	}

	/**
	 * 分享图片
	 *
	 * @param bitmap
	 * 	图片bitmao
	 * @param judge
	 * 	类型选择 好友-WECHAT_FRIEND 朋友圈-WECHAT_MOMENT
	 */
	public static void WxBitmapShare(Context context, Bitmap bitmap, int judge) {

		if (judgeCanGo(context)) {
			return;
		}
		WXImageObject wxImageObject = new WXImageObject(bitmap);
		WXMediaMessage message = new WXMediaMessage();
		message.mediaObject = wxImageObject;

		Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
		bitmap.recycle();
		message.thumbData = Util.bmpToByteArray(thunmpBmp, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = message;
		req.scene = judge;

		sWxApi.sendReq(req);
	}

	/**
	 * 网页分享
	 *
	 * @param url
	 * 	地址
	 * @param title
	 * 	标题
	 * @param description
	 * 	描述
	 * @param bitmap
	 * 	缩略图
	 * @param scene
	 * 	分享场景
	 */
	public static void WxUrlShare(Context context, String url, String title, String description,
	                              Bitmap bitmap, int scene) {

		if (judgeCanGo(context)) {
			return;
		}
		WXWebpageObject wxWebpageObject = new WXWebpageObject();
		wxWebpageObject.webpageUrl = url;

		WXMediaMessage wxMediaMessage = new WXMediaMessage(wxWebpageObject);
		wxMediaMessage.title = title;
		wxMediaMessage.description = description;
		if (bitmap != null) {
			Bitmap thunmpBmp = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
			bitmap.recycle();
			wxMediaMessage.thumbData = Util.bmpToByteArray(thunmpBmp, true);
		}

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = wxMediaMessage;
		req.scene = scene;

		sWxApi.sendReq(req);
	}

	private static boolean judgeCanGo(Context context) {

		initWxApi(context);
		if (!sWxApi.isWXAppInstalled()) {
			Toast.makeText(context, "请先安装微信应用", Toast.LENGTH_SHORT).show();
			return true;
		}
		// if (!sWxApi.isWXAppSupportAPI()) {
		// 	Toast.makeText(context, "请先更新微信应用", Toast.LENGTH_SHORT).show();
		// return true;
		// }
		return false;
	}

	private static void initWxApi(Context context) {

		if (sWxApi == null) {//true
			sWxApi = WXAPIFactory.createWXAPI(context, WX_APP_ID);
			sWxApi.registerApp(WX_APP_ID);
		}
	}
}